package com.imooc.town.handler;

import com.imooc.town.config.ProjectUrlConfig;
import com.imooc.town.exception.ResponseBankException;
import com.imooc.town.exception.SellException;
import com.imooc.town.exception.SellerAuthorizeException;
import com.imooc.town.utils.ResultVOUtil;
import com.imooc.town.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * create by song on 2018/3/26
 * To change this template use File | Settings | File Templates.
 */

@ControllerAdvice
public class SellExceptionHandler {

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    //拦截登录异常
    //http://sell.natapp4.cc/sell/wechat/qrAuthorize?returnUrl=http://sell.natapp4.cc/sell/seller/login
    @ExceptionHandler(value = SellerAuthorizeException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ModelAndView handlerAuthorizeException() {
        return new ModelAndView ( "redirect:"
                .concat ( projectUrlConfig.getWechatOpenAuthorize () )
                .concat ( "/sell/wechat/qrAuthorize" )
                .concat ( "?returnUrl=" )
                .concat ( projectUrlConfig.getSell () )
                .concat ( "/sell/seller/login" ) );

    }
    @ExceptionHandler(value = SellException.class)
    @ResponseBody
    public ResultVo handlerSellerException(SellException e){
        return ResultVOUtil.error ( e.getCode (),e.getMessage () );
    }

    @ExceptionHandler(value = ResponseBankException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlerResponsetBankException(){

    }


}
